package com.studentstreet.tools.dbhelper;

import java.lang.reflect.Type;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.studentstreet.R;
import com.studentstreet.entities.GoodsEntity;
import com.studentstreet.tools.Util;

/**
 * @author likebamboo
 * 
 */
public class GoodsDBHelper {
	private DBHelper dbHelper = null;
	// 标记收藏夹中又该商品，
	public static final int ADD_FAVORITE = R.drawable.favorite_s;
	// 标记收餐夹中无此商品
	public static final int MOVE_FAVORITE = R.drawable.favorite_d;

	public GoodsDBHelper(Context context) {
		super();
		dbHelper = new DBHelper(context);
	}

	public long saveToDB(String json) {
		Type listType = new TypeToken<ArrayList<GoodsEntity>>() {
		}.getType();
		// 创建Gson 对象
		Gson gson = new Gson();
		// 将Json数据转化为List<GoodsEntity>
		List<GoodsEntity> goodsList = gson.fromJson(json, listType);
		return saveToDB(goodsList);
	}

	// 将网上下载的数据保存到数据库中
	public long saveToDB(List<GoodsEntity> goodsList) {
		long row = 0L;
		SQLiteDatabase db = dbHelper.getReadableDatabase();
		for (GoodsEntity goods : goodsList) {
			deleteRow(goods.getId());
			ContentValues values = new ContentValues();
			values.put(DBTools.GOODS_ID, goods.getId());
			values.put(DBTools.GOODS_CID, goods.getClassName());
			values.put(DBTools.GOODS_DESC, goods.getDescrition());
			values.put(DBTools.GOODS_DISCOUNT, goods.getDiscount());
			values.put(DBTools.GOODS_IMG, goods.getImageUrl());
			values.put(DBTools.GOODS_KEYWORD, goods.getKeyWords());
			values.put(DBTools.GOODS_NAME, goods.getName());
			values.put(DBTools.GOODS_NUTRI, goods.getNutrition());
			values.put(DBTools.GOODS_PRICE, goods.getPrice());
			values.put(DBTools.GOODS_SALECLASS, goods.getSaleClass());
			values.put(DBTools.GOODS_SALECOUNT, goods.getSaleCount());
			values.put(DBTools.GOODS_SID, goods.getSellerId());
			row += db.insert(DBTools.TABLE_GOODS, null, values);
		}
		db.close();
		return row;
	}

	// 删除旧数据
	public long deleteRow(int goodsId) {
		long row = 0L;
		SQLiteDatabase db = dbHelper.getReadableDatabase();
		row = db.delete(DBTools.TABLE_GOODS,
				DBTools.GOODS_ID + " = " + goodsId, null);
		// db.close();
		return row;

	}

	// 将数据保存到收藏夹表
	public int saveToStore(GoodsEntity goods, String userName) {
		long row = 0L;
		int result = 0;
		SQLiteDatabase db = dbHelper.getReadableDatabase();
		ContentValues values = new ContentValues();
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd:hh:mm:ss");
		values.put(DBTools.STORE_ADDTIME, sdf.format(new Date()));
		values.put(DBTools.STORE_GID, goods.getId());
		values.put(DBTools.STORE_GNAME, goods.getName());
		values.put(DBTools.STORE_PRICE, goods.getPrice());
		// OEM++ 2013-02-27修正从收藏夹中添加商品到购物车无法获得商品所属商家Id的情况
		if (Util.OEM) {
			values.put(DBTools.STORE_SID, goods.getSellerId());
		}
		// OEM--
		values.put(DBTools.STORE_RANK, goods.getRank());
		values.put(DBTools.STORE_SNAME, goods.getSellerName());
		values.put(DBTools.STORE_GIMG, goods.getImageUrl());
		values.put(DBTools.STORE_SALE_COUNT, goods.getSaleCount());
		values.put(DBTools.STORE_UNAME, userName);

		Cursor cursor = db.query(DBTools.TABLE_STORE, null, DBTools.STORE_GID
				+ " = " + goods.getId() + " and " + DBTools.STORE_UNAME
				+ " like '" + userName + "'", null, null, null, null);
		// 如果收藏夹中没有该商品
		if (cursor.getCount() == 0) {
			row = db.insert(DBTools.TABLE_STORE, null, values);
			if (row != 0) {
				result = ADD_FAVORITE;
			}
		} else {
			row = db.delete(DBTools.TABLE_STORE, DBTools.STORE_GID + " = "
					+ goods.getId() + " and " + DBTools.STORE_UNAME + " like '"
					+ userName + "'", null);
			if (row != 0) {
				result = MOVE_FAVORITE;
			}
		}
		cursor.close();
		db.close();
		return result;
	}

	// 查询收藏夹中是否有某一商品
	public int isFavorite(int goodsId, String userName) {
		int result = MOVE_FAVORITE;
		SQLiteDatabase db = dbHelper.getReadableDatabase();
		Cursor cursor = db.query(DBTools.TABLE_STORE, null, DBTools.STORE_GID
				+ " = " + goodsId + " and " + DBTools.STORE_UNAME + " like '"
				+ userName + "'", null, null, null, null);
		// 如果收藏夹中有该商品
		if (cursor.getCount() != 0) {
			result = ADD_FAVORITE;
		}
		cursor.close();
		db.close();
		return result;
	}

	// 查询收藏夹中所有商品
	public List<GoodsEntity> favoriteList(String userName) {
		List<GoodsEntity> result = new ArrayList<GoodsEntity>();
		SQLiteDatabase db = dbHelper.getReadableDatabase();
		Cursor c = db.query(DBTools.TABLE_STORE, null, DBTools.STORE_UNAME
				+ " like '" + userName + "'", null, null, null, null);
		while (c.moveToNext()) {
			GoodsEntity goods = new GoodsEntity();
			goods.setId(c.getInt(c.getColumnIndex(DBTools.STORE_GID)));
			goods.setName(c.getString(c.getColumnIndex(DBTools.STORE_GNAME)));
			goods.setSellerName(c.getString(c
					.getColumnIndex(DBTools.STORE_SNAME)));
			goods.setPrice(c.getDouble(c
					.getColumnIndexOrThrow(DBTools.STORE_PRICE)));
			goods.setRank(c.getDouble(c.getColumnIndex(DBTools.STORE_RANK)));
			goods.setImageUrl(c.getString(c.getColumnIndex(DBTools.STORE_GIMG)));
			goods.setSaleCount(c.getInt(c
					.getColumnIndex(DBTools.STORE_SALE_COUNT)));
			// OEM++ 2013-02-27修正从收藏夹中添加商品到购物车无法获得商品所属商家Id的情况
			if (Util.OEM) {
				goods.setSellerId(c.getInt(c.getColumnIndex(DBTools.STORE_SID)));
			}
			// OEM--
			result.add(goods);
		}
		c.close();
		db.close();
		return result;
	}
}
